package com.wjy.game.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wjy.game.entites.User;
import com.wjy.game.entity.Player;
import com.wjy.game.enums.Operate;
import com.wjy.game.mapper.automatic.UserMapper;
import com.wjy.game.message.Request;
import com.wjy.game.message.Response;
import com.wjy.game.message.data.req.LoginReq;
import com.wjy.game.message.data.res.LoginRes;
import com.wjy.game.service.MessageHandleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author wjy
 * @date 2025/5/23 10:57
 * @description 登录
 */
@Component
public class LoginMessageHandleService implements MessageHandleService<LoginReq, LoginRes> {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean support(Operate operate) {
        return Operate.LOGIN == operate;
    }

    @Override
    public Response<LoginRes> handle(Request<LoginReq> request) {
        LoginReq loginReq = request.getObj();
        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUserId, loginReq.getPlayerId()));
        if (user == null) {
            return Response.fail(request.getTraceId(), "用户不存在");
        }
        if (!user.getPassword().equals(loginReq.getPassword())) {
            return Response.fail(request.getTraceId(), "用户名或密码错误");
        }
        LoginRes loginRes = new LoginRes();
        Player player = new Player();
        player.setPlayerId(user.getUserId());
        player.setPlayerName(user.getUserName());
        player.setWinCount(user.getWin());
        player.setLoseCount(user.getLose());
        player.setDrawCount(user.getDraw());
        player.setScore(user.getScore());
        loginRes.setPlayer(player);
        return Response.success(request.getTraceId(), loginRes);
    }
}
